
exports.handleBasicGroups = function(basicGroups,prefixStr){
    var sources = {};
    /**public组的对应关系*/
    var publicRelations = [];
    var result = {
        "views":{},
        "groups":{}
    };

    createBasicGroups(sources,prefixStr,basicGroups,result);
    createPublicGroups(sources,prefixStr,publicRelations,result);
    setViewGroups(prefixStr,publicRelations,result);

    var obj = handResult(result);
    return obj;
}

function createBasicGroups(sources,prefixStr,obj,result) {
    for(var a in obj) {
        //初始化各个view的multiGroups
        result.views[a] = [];
        //初始化各个基本组
        result.groups[prefixStr + a] = [];

        //手动添加一些资源到group
        if(a == "preload"){
            result.groups[prefixStr + a].push("chat_json");
            result.groups[prefixStr + a].push("errorCode_json");
            result.groups[prefixStr + a].push("bg_city_png");
        }

        //手动添加一些group到需要的view
        if(a=="main"){
            result.views[a].push("fontGroup");
        }
        if(a=="baccarat" || a=="baccMi" || a=="dragonTiger" || a=="multiBacc"){
            result.views[a].push("commonPoker");
        }
        if(a=="baccMi"){
            result.views[a].push("commonPokeMi");
        }
        if(a=="roulList"){
            result.views[a].push("commonRoulRoadPoint");
        }
        if(a=="roulette"){
            result.views[a].push("commonRoulRoadPoint");
            result.views[a].push("commonRoulPoint");
        }
        if(a=="sicbo"){
            result.views[a].push("commonSicDice");
        }
        if(a=="record"){
            result.views[a].push("commonPoker");
            result.views[a].push("commonRoulPoint");
            result.views[a].push("commonSicDice");
        }
        if(a=="baccarat" || a=="baccMi" || a=="dragonTiger" || a=="roulette"|| a=="sicbo"|| a=="multiBacc"){
            result.views[a].push("commonInjectChip");
            result.views[a].push("commonListChip");
        }
        if(a=="baccVideoPlayBack" ||a=="dragonVideoPlayBack" ||a=="roulVideoPlayBack" ||a=="sicVideoPlayBack"){
            result.views[a].push("commonInjectChip");
        }

        //将所有资源名和它被使用的组 存到sources
        for(var n=0;n<obj[a].length;n++){
            var sourceName = obj[a][n];
            if(!sources[sourceName]){
                sources[sourceName] = [];
            }
            sources[sourceName].push(a);
        }
    }
}

/**创建公共资源组*/
function createPublicGroups(sources,prefixStr,publicRelations,result) {
    for(var sourceName in sources) {
        var groups = sources[sourceName];

        //这个资源只被一个组使用
        if (groups.length == 1) {
            result.groups[prefixStr + groups[0]].push(sourceName);
        }
        //如果一个资源被多个组使用,这个资源拿出来放到公共组
        else {
            var publicgroupName = prefixStr + "pub";
            for (var i = 0; i < groups.length; i++) {
                publicgroupName += ("_"+groups[i]);
            }
            //使用 pc_pub0 pc_pub1 等名字来表示公共组，因为用字符串拼接的结果太长了
            if(publicRelations.indexOf(publicgroupName) == -1)publicRelations.push(publicgroupName);
            //pub的尾数
            var index = publicRelations.indexOf(publicgroupName);

            if (!result.groups[prefixStr + "pub" + index]) {
                result.groups[prefixStr + "pub" + index] = [];
            }
            result.groups[prefixStr + "pub" + index].push(sourceName);
        }
    }
}
/**为每个view设置 需要的基本组与公共组*/
function setViewGroups(prefixStr,publicRelations,result) {
    for(var a in result.views){
        for(var b in result.groups){
            //资源数大于0的组 才放进view
            if(result.groups[b].length >0){
                //view对应的基本组，直接放入view
                if(b == prefixStr + a) {
                    result.views[a].push(b);
                }
                else{
                    if(b.indexOf(prefixStr + "pub") == 0){
                        var index = -1;
                        if(prefixStr == "m_"){
                            index = b.substring(5);
                        }
                        else if(prefixStr == "pc_"){
                            index = b.substring(6);
                        }

                        //view需要用到的公共组
                        if(publicRelations[index].indexOf("_"+a) != -1){
                            result.views[a].push(b);
                        }
                    }
                }
            }
            // 删除资源数为0的组
            else{
                delete result.groups[b];
            }
        }
    }
}
/**对结果做格式转换*/
function handResult(result) {
    var obj = {};
    obj.views = {};

    for(var groupName in result.views){
        obj.views[groupName] = result.views[groupName].join(",")
    }

    obj.groups = [];
    for(var a in result.groups){
        obj.groups.push({"keys":result.groups[a].join(","),"name":a});
    }

    return obj;
}
